package com.xiaren.ui.edit;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.Serializable;
import java.util.*;
import java.util.List;

import javax.swing.*;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.UndoManager;

import com.xiaren.common.ImageManager;
import com.xiaren.common.ObjectConstant;
import com.xiaren.util.FileChooserUtil;

/**
 * 用于TextFile文本的内容
 *
 * @author xiaren
 * @主页 http://www.leaf123.cn/
 * @邮箱 xia5523@qq.com
 */
public class UndoFunction implements ActionListener, Serializable {
    public UndoManager um = new UndoManager();// 撤销管理类
    public JPopupMenu rightPm = null;
    JMenuItem jiCopy = null;
    JMenuItem jiCut = null;
    JMenuItem jiPaste = null;
    JMenuItem jiUndo = null;
    JMenuItem jiRedo = null;
    JMenuItem jiIcon = null;
    JMenuItem maxIcon = null;
    JMenu jMenuIcon = null; // 导出图片操作
    JMenuItem jiConPrase = null;//图片到剪切板
    JMenuItem jiConFile = null; //图片导出成文件
    JMenuItem jiExportTxt = null; //导出txt
    TextFile textFile = null;

    public UndoFunction(TextFile textFile) {
        super();
        this.textFile = textFile;
        init();
    }

    void init() {
        rightPm = new JPopupMenu();
        jiCopy = new JMenuItem("复制 Ctrl+C");
        jiCut = new JMenuItem("剪切 Ctrl+X");
        jiPaste = new JMenuItem("粘贴 Ctrl+V");
        jiUndo = new JMenuItem("撤销 Alt+Left");
        jiRedo = new JMenuItem("恢复 Alt+Right");
        maxIcon = new JMenuItem("最大窗口");
        jiIcon = new JMenuItem("插入图片");
        jMenuIcon = new JMenu("导出图片");
        jiConPrase = new JMenuItem("图片到剪切板");
        jiConFile = new JMenuItem("图片导出成文件");
        jMenuIcon.add(jiConPrase);
        jMenuIcon.add(jiConFile);
        jiExportTxt = new JMenuItem("导出成文件(txt)");

        jiCopy.addActionListener(this);
        jiCut.addActionListener(this);
        jiPaste.addActionListener(this);
        jiUndo.addActionListener(this);
        jiRedo.addActionListener(this);
        jiIcon.addActionListener(this);
        maxIcon.addActionListener(this);
        jiConPrase.addActionListener(this);
        jiConFile.addActionListener(this);
        jiExportTxt.addActionListener(this);
        rightPm.add(jiCopy);
        rightPm.add(jiCut);
        rightPm.add(jiPaste);
        rightPm.add(jiUndo);
        rightPm.add(jiRedo);
        // rightPm.add(jiIcon);
        rightPm.add(maxIcon);
        rightPm.add(jMenuIcon);
        rightPm.add(jiExportTxt);

        textFile.textPane.addMouseListener(new MouseAdapter() {
//            public void mousePressed(MouseEvent e) {
//                maybeShowPopup(e);
//            }

            public void mouseReleased(MouseEvent e) {
                maybeShowPopup(e);
            }

            private void maybeShowPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {// 如果有弹出菜单
                    if(textFile.getListDocumentImage().isEmpty()){
                        jMenuIcon.setEnabled(false);
                    } else {    // 有图片才会展现
                        jMenuIcon.setEnabled(true);
                    }
                    rightPm.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });
        textFile.textPane.getDocument().addUndoableEditListener(
                new UndoableEditListener() {// 注册撤销可编辑监听器
                    public void undoableEditHappened(UndoableEditEvent e) {
                        um.addEdit(e.getEdit());
                    }

                });// 编辑撤销的监听
    }

    public void actionPerformed(ActionEvent e) {
        try {
            if (e.getSource() == jiUndo && um.canUndo()) {// 撤销
                um.undo();
            } else if (e.getSource() == jiRedo && um.canRedo()) {// 恢复
                um.redo();
            } else if (e.getSource() == jiRedo) {// 插入图片
                // ImageIcon icon = new ImageIcon(getClass().getResource("1.png"));
                // fileEdit.textPane.insertIcon(icon);
            } else if (e.getSource() == maxIcon) {// 如果是窗体最大化
                new MaxFileFrame(textFile);
            } else if (e.getSource() == jiCopy) {// 如果是复制
                if (textFile.textPane.getSelectedText() != null) {
                    textFile.textPane.copy();
                }
            } else if (e.getSource() == jiCut) {// 如果是剪切
                if (textFile.textPane.getSelectedText() != null) {
                    textFile.textPane.cut();
                }
            } else if (e.getSource() == jiPaste) {// 如果是粘贴
                String pasteText = ImageManager.getClipboardText();
                if (pasteText != null) {
                    textFile.textPane.paste();
                }
                Image image = ImageManager.getImageFromClipboard();
                if (image == null) {// 如果不是图片，直接返回
                    return;
                }
                textFile.textPane.insertIcon(new ImageIcon(image));

            } else if (e.getSource() == jiConPrase) {// 如果是图片到剪切板
                ImageManager.setClipboardImage(textFile.getDocumentImage());
            } else if (e.getSource() == jiConFile) {// 如果是图片导出成文件
                String path = FileChooserUtil.saveImage(textFile.getDocumentImage(), textFile.fileName);
                int result = JOptionPane.showConfirmDialog(null, "是否打开图片文件夹");
                if (result == JOptionPane.OK_OPTION) {
                    String pathDir = path.substring(0, path.lastIndexOf("\\"));
                    Desktop.getDesktop().open(new File(pathDir));
                }
            } else if(e.getSource() == jiExportTxt){    // 导出txt
                String path = FileChooserUtil.exportFile(textFile.textPane.getText(), textFile.fileName);
                int result = JOptionPane.showConfirmDialog(null, "是否打开文本文件夹");
                if (result == JOptionPane.OK_OPTION) {
                    String pathDir = path.substring(0, path.lastIndexOf("\\"));
//                    Desktop.getDesktop().open(new File(pathDir));
//                    explorer /select,C:\Users\xiaren\Documents\rr.pngs
                    Runtime.getRuntime().exec("explorer /select," + path);
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }


    }

}
